<template>
  <el-drawer
    v-model="renderVisible"
    :title="props.title"
    destroy-on-close
    :size="props.size"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      ref="formRef"
      :label-position="'top'"
      label-width="160px"
      :model="formData"
      :rules="formRules"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="formData.title"
          :maxlength="32"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="title">
        <el-input
          v-model="formData.message"
          :maxlength="255"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="首页投放" prop="state">
        <el-switch
          v-model="formData.state"
          :active-value="1"
          :inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
      <el-form-item label="弹窗展示" prop="isAlert">
        <el-switch
          v-model="formData.isAlert"
          :active-value="1"
          :inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <PipeResetBtn :formEl="formRef">重置</PipeResetBtn>
      <el-button
        type="primary"
        @click="handleClickSubmit(formRef)"
        :loading="isLoading"
        >提交</el-button
      >
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { reactive, ref, watch, computed, PropType } from 'vue';
import type { ElForm } from 'element-plus';
import { ElMessage } from 'element-plus';

import { createNotify, updateNotify } from '@/apis/notify';
import { loopPropAssignment } from '@/utils/util';

// 加载状态
const isLoading = ref(false);

// 表单ref
const formRef = ref<InstanceType<typeof ElForm>>();

// 表单数据
const formData: API.notify.EntityDto = reactive({
  title: '',
  message: '',
  state: 1,
  isAlert: 1
});

const formRules = reactive({
  type: [{ type: 'string', required: true, message: '请选择类型' }],
  companyId: [{ type: 'number', required: true, message: '请选择运营公司' }],
  houseId: [{ type: 'number', required: true, message: '请选择房源' }],
  url: [{ type: 'string', required: true, message: '请上传广告图片' }],
  title: [{ type: 'string', required: true, message: '请输入广告标题' }]
  // title: [
  //   { type: "string", required: true, message: "请输入房源标题" },
  //   { max: 255, trigger: "blur", message: "长度不能超过255位字符" },
  // ],
  // description: [
  //   { type: "string", required: true, message: "请输入房源简介" },
  //   { max: 255, trigger: "blur", message: "长度不能超过255位字符" },
  // ],
});

// props
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },

  title: String,

  size: {
    type: [String, Number],
    default: '30%'
  },

  isEdit: {
    type: Boolean,
    default: false
  },

  rowData: {
    type: Object as PropType<API.notify.TableData>,
    default: () => ({})
  }
});

// emits
const emits = defineEmits(['update:modelValue', 'onRefresh']);

// computed - 抽屉[显示|隐藏]
const renderVisible = computed({
  get: () => props.modelValue,
  set: (val) => {
    emits('update:modelValue', val);
  }
});

// watch
watch(
  () => props.modelValue,
  (val) => {
    if (!val) return;
    // 当rowData发生变化，初始化数据
    loopPropAssignment(formData, props.rowData);
  },
  {
    immediate: true
  }
);

// btn - submit
const handleClickSubmit = (formEl: InstanceType<typeof ElForm> | undefined) => {
  if (!formEl) return;

  formEl.validate(async (valid) => {
    if (!valid) return;
    if (props.isEdit) {
      submitDataUpdate();
    } else {
      submitDataAdd();
    }
  });
};

// submit - add
const submitDataAdd = () => {
  isLoading.value = true;
  createNotify(formData)
    .then((res) => {
      isLoading.value = false;
      ElMessage.success(res.message);
      // 关闭弹窗
      emits('onRefresh');
      renderVisible.value = false;
    })
    .catch(() => {
      isLoading.value = false;
    });
};

// submit - update
const submitDataUpdate = () => {
  isLoading.value = true;
  updateNotify(props.rowData.id!, formData)
    .then((res) => {
      isLoading.value = false;
      ElMessage.success(res.message);
      // 关闭弹窗
      emits('onRefresh');
      renderVisible.value = false;
    })
    .catch(() => {
      isLoading.value = false;
    });
};
</script>

<style lang="scss" scoped></style>
